package com.leetcode.weekly.contest.no284th;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 覃国强
 * @date 2022-03-13 10:31
 */
public class A01_找出数组中的所有K近邻下标 {


  static
  class Solution {
    public List<Integer> findKDistantIndices(int[] nums, int key, int k) {

      List<Integer> answer = new ArrayList<>();

      int cur = 0;
      for (int i = 0; i < nums.length && cur < nums.length; ++i) {
        if (nums[i] == key) {
          int left = i - k;
          int right = i + k;
          left = Math.max(cur, left);
          right = Math.min(right, nums.length - 1);
          for (int j = left; j <= right; ++j) {
            answer.add(j);
          }
          cur = right + 1;
        }
      }

      return answer;
    }
  }

}
